// TempletPreviewPage.cpp : implementation file
//

#include "stdafx.h"
#include "kwclientcore.h"
#include "TempletPreviewPage.h"
#include "TempletItemPage.h"
#include "TempletPointPage.h"
#include "TempletTimePage.h"
#include "TempletHeadFootPage.h"
#include "TempletOptionPage.h"

#include "CommonLib\Structure.h"

#include "GUIBaseLib\TopPropertySheet.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTempletPreviewPage property page

IMPLEMENT_DYNCREATE(CTempletPreviewPage, CPropertyPage)

CTempletPreviewPage::CTempletPreviewPage() : CPropertyPage(CTempletPreviewPage::IDD)
{
	//{{AFX_DATA_INIT(CTempletPreviewPage)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_pReportTempletHeader = NULL;
}

CTempletPreviewPage::~CTempletPreviewPage()
{
  if (m_pReportTempletHeader) delete m_pReportTempletHeader;
}

void CTempletPreviewPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTempletPreviewPage)
	DDX_Control(pDX, IDC_TEMPLETPREVIEWPAGE_PLACEHOLDER, m_btnPlaceHolder);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CTempletPreviewPage, CPropertyPage)
	//{{AFX_MSG_MAP(CTempletPreviewPage)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTempletPreviewPage message handlers

BOOL CTempletPreviewPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	// TODO: Add extra initialization here
  CRect rect;
  m_btnPlaceHolder.GetWindowRect(&rect);
  ScreenToClient(&rect);
  m_wndPreview.CreateEx(WS_EX_CLIENTEDGE, 
                        AfxRegisterWndClass(CS_DBLCLKS, LoadCursor(NULL, IDC_ARROW), (HBRUSH)GetStockObject(WHITE_BRUSH)), 
                        _T("TempletPreviewWindow"), 
                        WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL | WS_TABSTOP, 
                        rect, this, 0);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CTempletPreviewPage::OnSetActive() 
{
	// TODO: Add your specialized code here and/or call the base class
  CTopPropertySheet * pSheet = (CTopPropertySheet *)GetParent();

  for (int i = 0;i < pSheet->GetPageCount()-1;i++)
  {
    CPropertyPage * pPage = pSheet->GetPage(i);
    ASSERT(pPage);
    if (IsWindow(pPage->GetSafeHwnd()))
    {
      pPage->UpdateData();
    }
  }

  ASSERT(pSheet->GetPage(0)->IsKindOf(RUNTIME_CLASS(CTempletItemPage)));
  CTempletItemPage * pTempletItemPage = (CTempletItemPage *)pSheet->GetPage(0);

  ASSERT(pSheet->GetPage(1)->IsKindOf(RUNTIME_CLASS(CTempletPointPage)));
  CTempletPointPage * pTempletPointPage = (CTempletPointPage *)pSheet->GetPage(1);

  ASSERT(pSheet->GetPage(2)->IsKindOf(RUNTIME_CLASS(CTempletTimePage)));
  CTempletTimePage * pTempletTimePage = (CTempletTimePage *)pSheet->GetPage(2);
  if (IsWindow(pTempletTimePage->GetSafeHwnd())) pTempletTimePage->UpdateExtendedData();

  ASSERT(pSheet->GetPage(3)->IsKindOf(RUNTIME_CLASS(CTempletHeadFootPage)));
  CTempletHeadFootPage * pTempletHeadFootPage = (CTempletHeadFootPage *)pSheet->GetPage(3);

  ASSERT(pSheet->GetPage(4)->IsKindOf(RUNTIME_CLASS(CTempletOptionPage)));
  CTempletOptionPage * pTempletOptionPage = (CTempletOptionPage *)pSheet->GetPage(4);
  if (IsWindow(pTempletOptionPage->GetSafeHwnd())) pTempletOptionPage->UpdateExtendedData();

  if (m_pReportTempletHeader) delete m_pReportTempletHeader;
	m_pReportTempletHeader = new ReportTempletHeader;

  m_pReportTempletHeader->m_nNumOfFields = pTempletItemPage->m_ReportFields.size();
  m_pReportTempletHeader->m_nNumOfMonUnits = pTempletPointPage->m_MonUnitItems.size();

  memcpy(&m_pReportTempletHeader->m_lfCell, &pTempletPointPage->m_lfCell, sizeof(LOGFONT));
  m_pReportTempletHeader->m_crCellFont = pTempletPointPage->m_crCellFont;
  m_pReportTempletHeader->m_nCellHeight = pTempletPointPage->m_nCellHeight;

  m_pReportTempletHeader->m_nTimeType = pTempletTimePage->m_nTimeType;
  pTempletTimePage->m_timeBeginTime.GetAsSystemTime(m_pReportTempletHeader->m_stBegin);
  pTempletTimePage->m_timeEndTime.GetAsSystemTime(m_pReportTempletHeader->m_stEnd);

  strcpy(m_pReportTempletHeader->m_szHeadText, pTempletHeadFootPage->m_strHeadText);
  memcpy(&m_pReportTempletHeader->m_lfHead, &pTempletHeadFootPage->m_lfHead, sizeof(LOGFONT));
  m_pReportTempletHeader->m_crHeadFont = pTempletHeadFootPage->m_crHeadFont;
  strcpy(m_pReportTempletHeader->m_szFootText, pTempletHeadFootPage->m_strFootText);
  memcpy(&m_pReportTempletHeader->m_lfFoot, &pTempletHeadFootPage->m_lfFoot, sizeof(LOGFONT));
  m_pReportTempletHeader->m_crFootFont = pTempletHeadFootPage->m_crFootFont;
  
  m_pReportTempletHeader->m_dwOptions = pTempletOptionPage->m_dwOptions;

  const std::vector<ReportField *>& ReportFields = pTempletItemPage->m_ReportFields;
  const std::vector<MonUnitItem *>& MonUnitItems = pTempletPointPage->m_MonUnitItems;

  m_wndPreview.ParamReady();
  m_wndPreview.SetFocus();

	return CPropertyPage::OnSetActive();
}

const std::vector<ReportField *>& CTempletPreviewPage::GetReportFields() const
{
  CTopPropertySheet * pSheet = (CTopPropertySheet *)GetParent();
  ASSERT(pSheet->GetPage(0)->IsKindOf(RUNTIME_CLASS(CTempletItemPage)));
  CTempletItemPage * pTempletItemPage = (CTempletItemPage *)pSheet->GetPage(0);
  return pTempletItemPage->m_ReportFields;
}

const std::vector<MonUnitItem *>& CTempletPreviewPage::GetMonUnitItems() const
{
  CTopPropertySheet * pSheet = (CTopPropertySheet *)GetParent();
  ASSERT(pSheet->GetPage(1)->IsKindOf(RUNTIME_CLASS(CTempletPointPage)));
  CTempletPointPage * pTempletPointPage = (CTempletPointPage *)pSheet->GetPage(1);
  return pTempletPointPage->m_MonUnitItems;
}
